package com.yin.myproject.arithmetic;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * @author Eason
 * @create 2018-04-11 17:47
 **/
public class BinarySearchDemo {

    public static Integer binarySearch(List<Integer> list, Integer key) {
        Collections.sort(list);
        int low = 0;
        int high = list.size();
        while (low < high) {
            int mid = (low + high) / 2;
            if (key == list.get(mid)) {
                return key;
            } else if (key > list.get(mid)) {
                low = mid + 1;
            } else {
                high = mid - 1;
            }
        }
        return Integer.MIN_VALUE;
    }

    public static void main(String[] args) {
        ArrayList list = new ArrayList<>();
        list.add(1);
        list.add(11);
        list.add(20);
        list.add(33);
        list.add(5);
        list.add(78);
        list.add(123);
        list.add(124);
        list.add(156);
        list.add(12);
        list.add(45);
        Integer integer = binarySearch(list, 123);
        System.out.println(integer);
    }
}
